package com.vhall.component.entity.goods.goods;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.vhall.component.framework.common.annotation.ParamName;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @author shimingming
 * @since 2023/5/31 10:51
 */
@Data
@NoArgsConstructor
public class GoodsPageVO<T extends Serializable> implements Serializable {


    @JsonProperty(value = "from")
    private Integer from;

    @JsonProperty(value = "to")
    private Integer to;

    @JsonProperty(value = "total")
    private Integer total;

    @JsonProperty(value = "page")
    private Integer currentPage;

    @JsonProperty(value = "last_page")
    private Integer lastPage;

    @JsonProperty(value = "per_page")
    private Integer perPage;

    @JsonProperty(value = "list")
    private List<T> data;

    @JsonProperty(value = "associated_num")
    @ParamName(value = "associated_num")
    private Integer associatedNum;


    public GoodsPageVO(Integer index, int size, int total, List<T> data, int associatedNum) {
        this.associatedNum = associatedNum;
        this.currentPage = index;
        this.total = total;
        this.perPage = size;
        this.data = data;
        this.from = data != null && data.size() > 0 ? (index - 1) * (size + 1) : 0;
        this.to = data != null && data.size() > 0 ? this.from + (data.size() - 1) : 0;
        this.lastPage = size == 0 ? 0 : Math.max((int) Math.ceil((double) total / (double) size), 1);
    }

}
